package com.combetter.changxing.fragment;

import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.combetter.changxing.R;
import com.combetter.changxing.base.BaseFragment;
import com.combetter.changxing.utils.MyUtils;
import com.combetter.changxing.utils.ToastUtils;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author Vany Liu
 * e-mail liu1281781414@outlook.com
 * Created ${date} ${time}
 * description : 设备Fragment
 **/
public class EquipmentFragment extends BaseFragment {
    @Bind(R.id.tv_title_name)
    TextView tvTitleName;
    @Bind(R.id.iv_back)
    ImageView ivBack;
    @Bind(R.id.iv_right)
    ImageView ivRight;
    @Bind(R.id.iv_right_add)
    ImageView ivRightAdd;
    @Bind(R.id.actionbar)
    Toolbar actionbar;

    @Bind(R.id.tv_equipment_matched)
    TextView tvEquipmentMatched;
    @Bind(R.id.rv_equipment_matched)
    RecyclerView rvEquipmentMatched;
    @Bind(R.id.tv_equipment_find)
    TextView tvEquipmentFind;
    @Bind(R.id.rv_equipment_find)
    RecyclerView rvEquipmentFind;

    /**
     * 模拟数据
     */
    List<String> matchedData = new ArrayList<>();
    List<String> findData = new ArrayList<>();

    @Override
    protected int layoutId() {
        return R.layout.fragment_equipment;
    }

    @Override
    protected void initViews(View view) {
        tvTitleName.setText("设备");
        ivBack.setVisibility(View.GONE);
        ivRight.setVisibility(View.GONE);
        ivRightAdd.setVisibility(View.VISIBLE);
        rvEquipmentMatched.setLayoutManager(new LinearLayoutManager(activity));
        rvEquipmentMatched.setAdapter(new CommonAdapter<String>(activity, R.layout.item_equipment_matched, matchedData) {
            @Override
            protected void convert(ViewHolder holder, String s, int position) {

                holder.setText(R.id.tv_sy_smoke, s);
                holder.setOnClickListener(R.id.rl_sy_add, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        initPopWindow();
                    }
                });
            }
        });
        rvEquipmentFind.setLayoutManager(new LinearLayoutManager(activity));
        rvEquipmentFind.setAdapter(new CommonAdapter<String>(activity, R.layout.item_equipment, findData) {
            @Override
            protected void convert(ViewHolder holder, String s, int position) {
                holder.setText(R.id.tv_item_equipment_name, s);
                holder.setOnClickListener(R.id.tv_item_equipment_name, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        initPopWindow();
                    }
                });
            }
        });
    }


    @Override
    protected void loadData() {
        //添加假数据
        matchedData.add("NBT ZERO2019001");
        matchedData.add("NBT ZERO2019002");
        matchedData.add("NBT ZERO2019003");
        //添加假数据
        findData.add("NBT ZERO2019004");
        findData.add("NBT ZERO2019005");
        findData.add("NBT ZERO2019006");

        tvEquipmentMatched.setText("已匹配设备(" + matchedData.size() + ")");
        tvEquipmentFind.setText("已找到设备(" + findData.size() + ")");

        rvEquipmentMatched.getAdapter().notifyDataSetChanged();
        rvEquipmentFind.getAdapter().notifyDataSetChanged();


    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @OnClick(R.id.iv_right_add)
    public void onViewClicked() {
        ToastUtils.normal("扫描完毕...");

    }

    private void initPopWindow() {
        PopupWindow popupWindowCode = new PopupWindow(activity);
        View view = LayoutInflater.from(activity).inflate(R.layout.dialog_edit, null);
        popupWindowCode.setContentView(view);
        popupWindowCode.setBackgroundDrawable(new BitmapDrawable());
        popupWindowCode.setFocusable(true);
        popupWindowCode.setTouchable(true);
        popupWindowCode.setOutsideTouchable(true);
        popupWindowCode.setAnimationStyle(R.style.take_photo_anim);
        MyUtils.backgroundAlpha(0.5f, activity);//设置屏幕透明度
        popupWindowCode.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWindowCode.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWindowCode.showAtLocation(ivBack, Gravity.CENTER, 0, 0);
        popupWindowCode.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                MyUtils.backgroundAlpha(1.0f, activity);//设置屏幕透明度
                popupWindowCode.dismiss();

            }
        });
    }
}
